#include "header/evhttp/httpserver.h"
#include "config.h"
#include <sys/stat.h>
#include "header/utils/utils.h"
#include "header/net/net_info.h"


void init_daemon(void);
void* pull_data_thread(void* arg);



int main()
{
   /* printf("%s\n","run!");
    int conections,i;
    conections = 0;
    Tcp_detail* tcpDetail ;
    tcpDetail = get_net_connections(&conections);
    printf("connections:%d\n",conections);
    for(i=0;i<conections;i++){
        Tcp_detail tcp = tcpDetail[i];
        printf("ip:%s,port:%s,l_port:%s\n",tcp.remote_ip,tcp.remote_port,tcp.local_port);
    }
    free(tcpDetail);
    printf("%s\n","done!");*/
    //init_daemon();
    //pull_data_thread();
 while(1){
        send_sys_info(SERVER_URL);
        sleep(PULL_SYS_INFO_INTEVAL);
    }
    return 0;
}

void* pull_data_thread(void* arg){
    while(1){
        send_sys_info(SERVER_URL);
        sleep(PULL_SYS_INFO_INTEVAL);
    }
}
void init_daemon(void)
{

	int pid;int i;
	if((pid=fork())>0)
		exit(0);
	else if(pid< 0)
		exit(1);
	setsid();
	if((pid=fork())>0)
		exit(0);
	else if(pid< 0)
		exit(1);

    if(already_running()){
        printf("daemon already_running!\n");
        exit(-3);
    }
    umask(0);
    for(i=0;i< 3;++i)
        close(i);
    chdir("/tmp");
   // pull_data_thread();
    pthread_t pull_thread;
    if(pthread_create(&pull_thread,NULL,(void*)pull_data_thread,NULL) !=0 ){
        perror("thread create error:");
        exit(1);
    }
    run_http_server(DEFAULT_PORT);
    exit(0);

}
